//
//  StreamViewCell.m
//  KoreaFlower
//
//  Created by Nicejinux on 12. 11. 10..
//  Copyright (c) 2012년 Nicejinux. All rights reserved.
//

#import "StreamViewCell.h"
#import <QuartzCore/QuartzCore.h>

@implementation StreamViewCell
{
    bool isMoved;
    bool started;
    UIImageView *imgView;
}

@synthesize label;
@synthesize image;
@synthesize delegate;
@synthesize reuseIdentifier;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        CGRect bgFrame = CGRectInset(self.bounds, 0.0f, 0.0f);
        UIImageView *bgView = [[UIImageView alloc] initWithFrame:bgFrame];
        bgView.layer.borderColor = [UIColor orangeColor].CGColor;
        bgView.layer.borderWidth = 1.0f;
        [self addSubview:bgView];
        bgView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        
        label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, bgFrame.size.width, 20)];
        label.center = CGPointMake(self.frame.size.width/ 2, self.frame.size.height/2);
        label.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
        label.backgroundColor = [UIColor clearColor];
        label.textAlignment = UITextAlignmentCenter;
        [self addSubview:label];
        
        bgView.image = image;
        imgView = bgView;
        isMoved = NO;
        UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
        tapRecognizer.delegate = self;
        [bgView addGestureRecognizer:tapRecognizer];
    }
    return self;
}

- (void)drawShadow
{
    imgView.layer.shadowColor = [UIColor grayColor].CGColor;
    imgView.layer.shadowOpacity = 0.3f;
    imgView.layer.shadowOffset = CGSizeMake(0, 0);
    imgView.layer.shadowRadius = 0.0f;
    imgView.layer.masksToBounds = NO;
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:imgView.bounds];
    imgView.layer.shadowPath = path.CGPath;
}

- (void)hideShadow
{
    imgView.layer.shadowColor = [UIColor clearColor].CGColor;
    imgView.layer.shadowOpacity = 0.0f;
    imgView.layer.shadowOffset = CGSizeMake(0.0f, 0);
    imgView.layer.shadowRadius = 0.0f;
    imgView.layer.masksToBounds = NO;
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:imgView.bounds];
    imgView.layer.shadowPath = path.CGPath;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    started = YES;
    imgView.layer.borderColor = [UIColor redColor].CGColor;
    imgView.layer.borderWidth = 2.0f;
    [self drawShadow];
    NSLog(@"touchesBegan");
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    started = NO;
    NSLog(@"touchesCancelled");
    imgView.layer.borderColor = [UIColor orangeColor].CGColor;
    imgView.layer.borderWidth = 1.0f;
    [self hideShadow];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    started = NO;
    NSLog(@"touchesEnded");
    if (!isMoved)
    {
        if ([self.delegate respondsToSelector:@selector(streamViewDidSelected:)] ) {
            [delegate streamViewDidSelected:[label.text intValue]];
        }
    }
    imgView.layer.borderColor = [UIColor orangeColor].CGColor;
    imgView.layer.borderWidth = 1.0f;
    [self hideShadow];
    isMoved = NO;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    isMoved = YES;
    imgView.layer.borderColor = [UIColor orangeColor].CGColor;
    imgView.layer.borderWidth = 1.0f;
    [self hideShadow];
    NSLog(@"touchesMoved");
}

- (void)handleTap:(UITapGestureRecognizer *)recognizer
{
    // Do Your thing.
    if (recognizer.state == UIGestureRecognizerStateEnded)
    {
        if ([self.delegate respondsToSelector:@selector(streamViewDidSelected:)] ) {
            [delegate streamViewDidSelected:[label.text intValue]];
        }
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
